<?php
// +----------------------------------------------------------------------+
// | WR Time Tracker
// +----------------------------------------------------------------------+
// | Copyright (c) 2004-2006 WR Consulting (http://wrconsulting.com)
// +----------------------------------------------------------------------+
// | LIBERAL FREEWARE LICENSE: This source code document may be used
// | by anyone for any purpose, and freely redistributed alone or in
// | combination with other software, provided that the license is obeyed.
// |
// | There are only two ways to violate the license:
// |
// | 1. To redistribute this code in source form, with the copyright
// |    notice or license removed or altered. (Distributing in compiled
// |    forms without embedded copyright notices is permitted).
// |
// | 2. To redistribute modified versions of this code in *any* form
// |    that bears insufficient indications that the modifications are
// |    not the work of the original author(s).
// |
// | This license applies to this document only, not any other software
// | that it may be combined with.
// |
// +----------------------------------------------------------------------+
// | Contributors: Igor Melnik <igor.melnik@mail.ru>
// +----------------------------------------------------------------------+

class I18n {
    
    var $mLang = "en";
    var $mDefaultLang = "en";
    var $cDateFormat = "d.m.Y";
    var $cDateTimeFormat = "d.m.Y H:i:s";
    var $cFloatDelimiter = ",";
    var $cCharSet = "";
    var $cTimeString = "";
    var $cMonthNames;
    var $cWeekDayNames;
    var $cWeekDayShortNames;
    var $cStartWeek;
    var $cHolidays;
    
    var $mKeys = array();
    var $mSourceDir = "";
    
    function I18n() {
    	$this->mKeys["test"] = array("one"=>"word");
    }
    
    function setSourceDir($dir) {
    	$this->mSourceDir = $dir;
    }
    
    function getKeys() {
    	return $this->mKeys;
    }
    
    function getKey($kword) {
    	$value = "";
    	$pos = strpos($kword, ".");
		if (!($pos === false)) {
           $p = explode(".", $kword);
           $str = "";
           foreach ($p as $w) {
               $str .= "[\"".$w."\"]";
           }
           eval("\$value = @\$this->mKeys".$str.";");
		} else {
           @$value = $this->mKeys[$kword];
		}
    	return $value;
    }
    
    function getKeyWithVars($kword, $arg0="", $arg1="", $arg2="") {
    	$value = "";
    	$pos = strpos($kword, ".");
		if (!($pos === false)) {
           $p = explode(".", $kword);
           $str = "";
           foreach ($p as $w) {
               $str .= "[\"".$w."\"]";
           }
           eval("\$value = @\$this->mKeys".$str.";");
		} else {
           @$value = $this->mKeys[$kword];
		}
		
		// Reemplazo de Variables si Existen
    	$patterns = array ("/\{0\}/","/\{1\}/","/\{2\}/");
		$replace = array ($arg0, $arg1, $arg2);
		$value = preg_replace ($patterns, $replace, $value);

    	return $value;
    }
    
    function getDateFormat() {
    	return $this->cDateFormat;
    }
    
    function getDateTimeFormat() {
    	return $this->cDateTimeFormat;
    }

    function getFloatDelimiter() {
    	return $this->cFloatDelimiter;
    }
    
    function getCharSet() {
    	return $this->cCharSet;
    }
    
    function getTimeString() {
    	return $this->cTimeString;
    }
    
    function getMonthName($id) {
    	$id = intval($id);
    	return $this->cMonthNames[$id];
    }
    
    function getMonthes() {
    	return $this->cMonthNames;
    }
    
    function getWeekDayShortNames() {
    	return $this->cWeekDayShortNames;
    }
    
    function getWeekDayName($id) {
    	$id = intval($id);
    	return $this->cWeekDayNames[$id];
    }

        
    function load($localName) {
		$kw = array();
		$filename = strtolower($localName) . '.lang.php';
		$inc_filename = $this->mSourceDir . '/' . $this->mDefaultLang . '.lang.php';

		if (file_exists($inc_filename)) {
			include_once($inc_filename);
			
			$this->cCharSet = $i18n_charset;
			$this->cTimeString = $i18n_timestring;
			$this->cMonthNames = $i18n_month;
    		$this->cWeekDayNames = $i18n_week;
    		$this->cWeekDayShortNames = $i18n_week_sn;
    		$this->cStartWeek = $i18n_start_week;
    		$this->cDateFormat = $i18n_format_date;
			$this->cDateTimeFormat = $i18n_format_date . " " . $i18n_format_time;
			$this->cFloatDelimiter = $i18n_float_delimiter;
			
			
			if (defined("SHOW_HOLIDAYS") && SHOW_HOLIDAYS == "true") {
				$this->cHolidays = @$i18n_holidays;
			}
			foreach ($i18n_key_words as $kword=>$value) {
			   $pos = strpos($kword, ".");
			   if (!($pos === false)) {
                   $p = explode(".", $kword);
                   $str = "";
                   foreach ($p as $w) {
                       $str .= "[\"".$w."\"]";
                   }
                   $value = str_replace("'","&rsquo;",$value);
                   eval("\$this->mKeys".$str."='".$value."';");
               } else {
                   $this->mKeys[$kword] = $value;
               }
			}
			$this->mKeys["float_delimiter"] = $i18n_float_delimiter;
		}
		
		$inc_filename = $this->mSourceDir . '/' . $filename;
		if (file_exists($inc_filename) && $localName!=$this->mDefaultLang) {
			require_once($inc_filename);
			
			$this->cCharSet = $i18n_charset;
			$this->cTimeString = $i18n_timestring;
			$this->cMonthNames = $i18n_month;
    		$this->cWeekDayNames = $i18n_week;
    		$this->cWeekDayShortNames = $i18n_week_sn;
    		$this->cStartWeek = $i18n_start_week;
    		$this->cDateFormat = $i18n_format_date;
			$this->cDateTimeFormat = $i18n_format_date . " " . $i18n_format_time;
			$this->cFloatDelimiter = $i18n_float_delimiter;
			
			
			if (defined("SHOW_HOLIDAYS") && SHOW_HOLIDAYS == "true") {
				$this->cHolidays = @$i18n_holidays;
			}
			foreach ($i18n_key_words as $kword=>$value) {
			   if (!$value) continue;
			   $pos = strpos($kword, ".");
			   if (!($pos === false)) {
                   $p = explode(".", $kword);
                   $str = "";
                   foreach ($p as $w) {
                       $str .= "[\"".$w."\"]";
                   }
                   $value = str_replace("'","&rsquo;",$value);
                   eval("\$this->mKeys".$str."='".$value."';");
               } else {
                   $this->mKeys[$kword] = $value;
               }
			}
			$this->mKeys["float_delimiter"] = $i18n_float_delimiter;
    		return true;
		}

	}
}
?>